<?php
	/**
	 * 获取签到抽奖详细
	 */
    class LotteryDrawDetail extends APIBase {
		
		public $tag = "LotteryDrawDetail";
    	public $isLogin = true;
		
		public function before() {
			$this->initCacheRedis();
			$this->initMysql();
			return true;
		}
		public function logic(){
			$playerID = $this->uid;
			$chance = array();
			$sort = array();
			$time = time();
			$lotterylist = $this->cache_redis->keys("hrewardpool:*");
			
		
            $R = mt_rand(1,100);//产生一个随机数
			$interval = array();//中奖区间

			if(count($lotterylist)>0){
				foreach($lotterylist as $key=>$val){
				    $list = $this->cache_redis->hGetAll($val);
					$chance[] = (int)$list['chance'];
					$sort[] = (int)$list['sort'];
					$ID[] = (int)$list['id'];
					$mid[] = (int)$list['mid'];
			    }
				
				sort($chance);

				$min = 0;
				$max = 100;
				//生成中奖区间
				for($i=0;$i<count($chance);$i++){
					if($i==0){
						$max = $min+$chance[$i];
						$interval[] = array($min,$max,$sort[$i],$mid[$i],$ID[$i]);
					}else{
						$min = $max;
						$max = $min+$chance[$i];
						$interval[] = array($min,$max,$sort[$i],$mid[$i],$ID[$i]);
					}
				}
				for($i=0;$i<count($interval);$i++){
					if($R>$interval[$i][0] && $R<=$interval[$i][1]){
						//echo $R;echo "</bt>";
						//var_dump($interval[$i]);echo "</bt>";
						$mid = $interval[$i][3];
						$rewardid = $interval[$i][4];
						//$this->mysql->query("insert mail_send (uid,mid,is_read,is_receive,send_date,update_date,rewardid,type) values($playerID,$mid,0,0,$time,$time,$rewardid,3)");
						$usermail = $this->cache_redis->keys("hUserEmailList:".$playerID.":*");
						if(empty($usermail)){
					        $count = 0;
			                $this->cache_redis->hMSet("hUserEmailList:".$playerID.":".$count,array('id'=>1,'uid'=>$playerID,'mid'=>$mid,'readstatus'=>0,'receivestatus'=>0,'date'=>$time,'update_date'=>$time,'rewardid'=>$rewardid,'type'=>3));
				        }else{
						    $count = count($usermail);
						    $this->cache_redis->hMSet("hUserEmailList:".$playerID.":".$count,array('id'=>$count+1,'uid'=>$playerID,'mid'=>$mid,'readstatus'=>0,'receivestatus'=>0,'date'=>$time,'update_date'=>$time,'rewardid'=>$rewardid,'type'=>3));
						}
						$this->returnData($interval[$i][4]);
					}
				}
			}else{
				$this->returnError(301,'请初始化奖励池！');
			}
			
		}
        public function after() {
			$this->deinitCacheRedis();
		}
    }
?>
